package com.wyp.didishige.bean;

import android.text.TextUtils;

import com.wyp.didishige.db.NewComment;
import com.wyp.didishige.model.CommentV3;

import org.json.JSONObject;

import cn.bmob.newim.bean.BmobIMExtraMessage;
import cn.bmob.newim.bean.BmobIMMessage;

/**
 * 评论消息
 * Created by horrywu on 16/5/24.
 */
public class SendCommentMessage extends BmobIMExtraMessage {

    public SendCommentMessage(){}

    /**
     * 将BmobIMMessage转成CommentV3
     * @param msg
     * @return
     */
    public static NewComment convert(BmobIMMessage msg){
        NewComment add =new NewComment();
        String content = msg.getContent();
        add.setTime(msg.getCreateTime());
        try {
            String extra = msg.getExtra();
            if(!TextUtils.isEmpty(extra)){
                JSONObject json =new JSONObject(extra);
                String name = json.getString("name");
                add.setName(name);
                String avatar = json.getString("avatar");
                add.setAvatar(avatar);
                add.setDynamicId(json.getString("dynamicId"));
                add.setContent(json.getString("content"));
                add.setType(json.getInt("type"));
                add.setCommentId(json.getString("commentId"));
            }else{
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return add;
    }


    @Override
    public String getMsgType() {
        return "comment";
    }

    @Override
    public boolean isTransient() {
        //设置为true,表明为暂态消息，那么这条消息并不会保存到本地db中，SDK只负责发送出去
        //设置为false,则会保存到指定会话的数据库中
        return true;
    }
}
